using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.eforceglobal.crux.controllers.Administration;
using com.eforceglobal.crux.DTOs;
using com.eforceglobal.crux.DTOs.Configuration;
using com.eforceglobal.crux.views.Administration;
using com.eforceglobal.crux.views.Enums;
using com.eforceglobal.crux.configuration;

public partial class Administration_ManageRoles : BaseView, IManageRoles
{
    ManageRolesController _controller;

    protected void Page_Load(object sender, EventArgs e)
    {
        int startingRowCount = Convert.ToInt32(CruxConfigurationManager.GetApplicationCustomSetting("PagerSize").Value);
        TableSortSearchPagination1.NumberOfRowsCsv = string.Format("{0},{1},{2},{3}", startingRowCount,
                                                                   startingRowCount * 2, startingRowCount * 4, startingRowCount * 6);
        _controller = new ManageRolesController(this);
        _controller.InitView(IsPostBack);
        if (!IsPostBack)
        {
            #region Site Map
            UrlTreeNodeItems.SetSiteNavigation(PageSiteMap, UrlTreeNodeItems.ManageRole);
            #endregion
            Page.DataBind();
        }
    }

    protected void btnAddNewRole_Click(object sender, ImageClickEventArgs e)
    {
        #region Site Map
        UrlTreeNodeItems.SetSiteNavigation(PageSiteMap, UrlTreeNodeItems.CreateLeafUrlTreeNode("Add", UrlTreeNodeItems.ManageRole));
        #endregion
        _controller.OpenRole(0);
    }

    protected void grdRoles_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton deleteButton = (ImageButton)e.Row.FindControl("btnItemDelete");
            string ReplaceConfirmation = new UILiterals().DeleteRoleConfirmation.Replace("{0}",
                DataBinder.Eval(e.Row.DataItem, "RoleName").ToString());
            ReplaceConfirmation = LocalizationManager.GetLocalizedText(ReplaceConfirmation);
            deleteButton.Attributes.Add("onclick", "javascript:return "
                + "confirm('" + ReplaceConfirmation + "')");

            Panel pnlEditDeleteRole = (Panel)e.Row.FindControl("pnlEditDeleteRole");
            if (IsEditRoleSupported) pnlEditDeleteRole.Visible = true;
            else pnlEditDeleteRole.Visible = false;
        }
    }

    protected void grdRoles_RowCommand(object sender, CommandEventArgs e)
    {
        string roleID = e.CommandArgument.ToString();
        RowCommands rowCommand = (RowCommands)Enum.Parse(typeof(RowCommands), e.CommandName);
        if (rowCommand == RowCommands.EditItem)
        {
            _controller.OpenRole(int.Parse(roleID));
            UrlTreeNodeItems.SetSiteNavigation(PageSiteMap, UrlTreeNodeItems.CreateLeafUrlTreeNode("Edit - " + CurrentRole.RoleName, UrlTreeNodeItems.ManageRole));
        }
        else if (rowCommand == RowCommands.DeleteItem)
            _controller.DeleteRole(int.Parse(roleID));
        else if (rowCommand == RowCommands.OpenView)
            _controller.OpenRoleAdministrationView(int.Parse(roleID));
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        _controller.SaveRole();
        #region Site Map
        if (FormMode == FormMode.List)
            UrlTreeNodeItems.SetSiteNavigation(PageSiteMap, UrlTreeNodeItems.ManageRole);
        #endregion
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        _controller.CancelAddEdit();
        #region Site Map
        UrlTreeNodeItems.SetSiteNavigation(PageSiteMap, UrlTreeNodeItems.ManageRole);
        #endregion
    }

    #region IManageRoles Members

    public List<RoleInformation> Roles
    {
        get
        {
            return (List<RoleInformation>)grdRoles.DataSource;

        }
        set
        {
            grdRoles.DataSource = value;
            grdRoles.DataBind();
        }
    }

    public RoleInformation CurrentRole
    {
        get
        {
            RoleInformation roleInformation = new RoleInformation();
            roleInformation.RoleID = int.Parse(lblRoleIDValue.Text);
            roleInformation.RoleName = txtRoleName.Text;
            roleInformation.RoleDescription = txtRoleDescription.Text;
            roleInformation.Comments = txtComments.Text;
            //roleInformation.Privilages = GetSelectedPrivilagesToRepresent();
            //roleInformation.CompanyTypes = GetSelectedCompanyTypesToRepresent();
            return roleInformation;
        }
        set
        {
            lblRoleIDValue.Text = value.RoleID.ToString();
            txtRoleName.Text = value.RoleName;
            txtRoleDescription.Text = value.RoleDescription;
            txtComments.Text = value.Comments;
            PopulateCompanyTypesToRepresentValues(value.CompanyTypes);
            PopulatePrivilagesToRepresentValues(value.Privilages);
        }
    }

    public List<CompanyTypeInformation> AllPossibleCompanyTypesToRepresent
    {
        set
        {
            chkCompanyTypes.DataSource = value;
            chkCompanyTypes.DataTextField = "CompanyTypeName";
            chkCompanyTypes.DataValueField = "CompanyTypeID";
            chkCompanyTypes.DataBind();
        }
    }

    public List<PrivilageInformation> AllPossiblePrivilagesToRepresent
    {
        set
        {
            chkPrivilages.DataSource = value;
            chkPrivilages.DataTextField = "PrivilageName";
            chkPrivilages.DataValueField = "PrivilageID";
            chkPrivilages.DataBind();
        }
    }

    public List<int> SelectedPrivilagesIdList
    {
        get { return GetSelectedPrivilageIDs(); }

    }

    public List<int> SelectedCompanyTypesIdList
    {
        get { return GetSelectedCompanyTypeIDs(); }

    }

    public FormMode FormMode
    {
        get
        {
            if (pnlList.Visible)
                return FormMode.List;
            if (pnlAddEdit.Visible)
                return FormMode.AddEdit;
            throw UIExceptions.InvalidFormModeException;
        }
        set
        {
            if (value == FormMode.List)
            {
                pnlList.Visible = true;
                pnlAddEdit.Visible = false;
            }
            else if (value == FormMode.AddEdit)
            {
                pnlList.Visible = false;
                pnlAddEdit.Visible = true;
            }
            else
            {
                throw UIExceptions.InvalidFormModeException;
            }
        }
    }

    public ErrorInformation ErrorDetails
    {
        get
        {
            return pnlError.ErrorInformation;
        }
        set
        {
            pnlError.ErrorInformation = value;
        }
    }

    #endregion

    public List<int> GetSelectedPrivilageIDs()
    {
        List<int> lstPrivilageIds = new List<int>();

        foreach (ListItem chkPrivilage in chkPrivilages.Items)
        {
            if (chkPrivilage.Selected)
                lstPrivilageIds.Add(Convert.ToInt32(chkPrivilage.Value));
        }
        return lstPrivilageIds;
    }

    public List<int> GetSelectedCompanyTypeIDs()
    {
        List<int> lstCompanyTypeIds = new List<int>();

        foreach (ListItem chkCompanyType in chkCompanyTypes.Items)
        {
            if (chkCompanyType.Selected)
                lstCompanyTypeIds.Add(Convert.ToInt32(chkCompanyType.Value));
        }
        return lstCompanyTypeIds;
    }

    private void PopulateCompanyTypesToRepresentValues(List<CompanyTypeInformation> list)
    {
        foreach (ListItem item in chkCompanyTypes.Items)
        {
            if (list.SingleOrDefault(p => p.CompanyTypeID == int.Parse(item.Value)) != null)
                item.Selected = true;
        }
    }

    private void PopulatePrivilagesToRepresentValues(List<PrivilageInformation> list)
    {
        foreach (ListItem item in chkPrivilages.Items)
        {
            if (list.SingleOrDefault(p => p.PrivilageID == int.Parse(item.Value)) != null)
                item.Selected = true;
        }
    }
    
    public bool IsAddRoleSupported
    {
        get { return pnlAddRole.Visible; }
        set { pnlAddRole.Visible = value; }
    }

    public bool IsEditRoleSupported
    {
        get
        {
            return grdRoles.Columns[2].Visible;

        }
        set
        {
            grdRoles.Columns[2].Visible = value;
        }
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        _controller.RedirectToPreviousPage();
    }
}
